﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Citi.ATM.Domain.Sharepoint.ATM;
using System.Net;
using System.Data.Services.Client;

namespace Citi.ATM.Domain
{
    public class ATMEvents
    {
        public static ATMServicesDataContext dataContext;

        public static ATMServicesDataContext DataContext
        {
            get
            {
                if (dataContext == null)
                    dataContext = new ATMServicesDataContext(new Uri("https://globalconsumer.collaborationtools.consumer.citigroup.net/sites/lac-brazil-corp-sites/ot/cti/operations/ctiatm/_vti_bin/ListData.svc"));

                dataContext.Credentials = CredentialCache.DefaultNetworkCredentials;

                return dataContext;
            }
        }

        public IEnumerable<EventosAbertosItem> GetAllEvents()
        {
            var response = DataContext.EventosAbertos.Execute() as QueryOperationResponse<EventosAbertosItem>;
            var cloudsList = response.ToList();

            try
            {
                QueryOperationResponse<EventosAbertosItem> responseCont = null;
                var token = response.GetContinuation();

                while (token != null)
                {
                    responseCont = DataContext.Execute<EventosAbertosItem>(token) as QueryOperationResponse<EventosAbertosItem>;

                    cloudsList.AddRange(responseCont);
                    token = responseCont.GetContinuation();
                }
            }
            catch
            {

            }

            return cloudsList;
        }

        public IEnumerable<EventosAbertosItem> GetByStatus(string status)
        {
            return GetAllEvents().Where(x => x.StatusDoChamadoValue == status);
        }

        public IEnumerable<EventosAbertosItem> GetIncidentVersusBranch(DateTime date)
        {
            return GetAllEvents().Where(x => x.Data.HasValue && x.Data.Value >= date).OrderBy(x => x.Nome);
        }
    }
}
